<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <jsp:include page="/resources/inc/head.jsp" />
</head>
<body>
<div class="layui-field-box">
    当前品牌：${brand.name}
</div>
<div class="zTreeDemoBackground left">
    <ul id="tree" class="ztree"></ul>
</div>
<footer>
    <div class="layui-input-block" style="margin-left: 20px;margin-top: 20px;">
        <button class="layui-btn" onclick="subCategory()">立即提交</button>
    </div>
</footer>
<jsp:include page="/resources/inc/footer.jsp" />
<script>
    var brandId = "${brand.id}";
    var zNodes = [{id:0,name:"顶级",open:true,nocheck:true}];
    <c:forEach var="productCategory" items="${productCategoryList}">
        zNodes.push({id:${productCategory.id},pId:${productCategory.pid},name:"${productCategory.name}"});
    </c:forEach>
    var setting = {
        check: {
            enable:true,
            chkboxType:{ "Y" : "", "N" : "" }
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    $(document).ready(function(){
        $.fn.zTree.init($("#tree"), setting, zNodes);
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        //将已选择的节点打钩
        <c:forEach var="selected" items="${selectedList}">
        treeObj.checkNode(treeObj.getNodeByParam("id", "${selected.id}", null), true, false);
        </c:forEach>
    });
    function subCategory() {
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getCheckedNodes(true);
        var ids = [];
        nodes.forEach(function(value, index, array) {
            ids.push(value.id);
        });
        var param = {brandId:brandId,ids:ids};
        $.ajax({
            type: 'POST',
            url: "${basePath}/manage/product/brand/category",
            data: JSON.stringify(param),
            contentType: "application/json;charset=utf-8",
            success: function (res) {
                if (res.data == 0) {
                    layer.msg(res.message, {icon: 5});
                } else {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                }
            },
            error: function (res) {
                layer.msg('删除失败！', {icon: 5});
            }
        });
    }
</script>
</body>
</html>
